package cn.andrewlu.weexshell.ui.widgetes;

import android.content.Context;
import android.util.Log;
import android.view.View;

import com.taobao.weex.IWXRenderListener;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.utils.WXFileUtils;

import cn.andrewlu.weexshell.beans.WXPage;
import cn.andrewlu.weexshell.ui.ILifeCycleChangeListener;

/**
 * Created by andrewlu on 2017/6/11.
 */

public class WXPageContainer implements IWXRenderListener, ILifeCycleChangeListener {
    protected WXSDKInstance mWXSDKInstance;
    private WXPage mPageInfo;
    private Context mContext;

    public WXPageContainer(Context context, WXPage page) {
        mWXSDKInstance = new WXSDKInstance(context);
        mWXSDKInstance.registerRenderListener(this);
        mPageInfo = page;
        mContext = context;
        loadPage();
    }

    private void loadPage() {
        if (mPageInfo.getAssets() != null) {
            mWXSDKInstance.render(WXFileUtils.loadAsset(mPageInfo.getAssets(), mContext));
        }
    }

    public WXSDKInstance getWXInstance() {
        return mWXSDKInstance;
    }

    public View getView() {
        return mWXSDKInstance.getContainerView();
    }

    @Override
    public void onViewCreated(WXSDKInstance instance, View view) {

    }

    @Override
    public void onRenderSuccess(WXSDKInstance instance, int width, int height) {

    }

    @Override
    public void onRefreshSuccess(WXSDKInstance instance, int width, int height) {

    }

    @Override
    public void onException(WXSDKInstance instance, String errCode, String msg) {

    }


    @Override
    public void onActivityCreate() {
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityCreate();
        }
        Log.d("WXPageContainer","onActivityCreate");
    }

    @Override
    public void onActivityStart() {
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityStart();
        }
        Log.d("WXPageContainer","onActivityStart");
    }

    @Override
    public void onActivityPause() {
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityPause();
        }
        Log.d("WXPageContainer","onActivityPause");
    }

    @Override
    public void onActivityResume() {
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityResume();
        }
        Log.d("WXPageContainer","onActivityResume");
    }

    @Override
    public void onActivityStop() {
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityStop();
        }
        Log.d("WXPageContainer","onActivityStop");
    }

    @Override
    public void onActivityDestroy() {
        if (mWXSDKInstance != null) {
            mWXSDKInstance.onActivityDestroy();
        }
        Log.d("WXPageContainer","onActivityDestroy");
    }

    @Override
    public boolean onActivityBack() {
        return false;
    }
}
